package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.qyweixin.AccessTokenDto;
import com.dingreading.cloud.common.util.qyweixin.QyWeiXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
@RequestMapping("/system/qiyeweixin")
@Api(tags = "总部-企业微信群发消息管理")
public class QIyeWeixinController {

    @Value("${qyweixin.corpid}")
    private String corpid;
    @Value("${qyweixin.secret}")
    private String secret;
    @Value("${qyweixin.agentid}")
    private String agentid;


    @ApiOperation(value = "给用户发消息")
    @PostMapping(value = "/sendToUser", produces = "application/json; charset=UTF-8")
    public R<Object> sendToUser(
            @ApiParam(value = "企业微信用户id") @RequestParam(value = "userid") String userid
    ) {
        AccessTokenDto accessTokenDto = QyWeiXinUtil.getAccessToken(corpid, secret);
        if (accessTokenDto != null) {
            QyWeiXinUtil.sendToUser(accessTokenDto.getAccess_token(), agentid, userid, "你好");
        }
        return R.ok();
    }

}
